#ifndef _UTILS_H
#define _UTILS_H

#include <string>
#include <sstream>
#include <vector>

template <typename T>
std::string tostr(T val)
{
	std::stringstream s;
	s << val;
	return s.str();
}

template <typename T>
T fromstr(const std::string & str)
{
	T val;
	std::stringstream s(str);
	s >> val;
	return val;
}

template <typename T>
void arrayfromstr(const std::string & str, T * array, int arraysize, char delim = ',')
{
	std::string strstrip = str;
	
	// replace delimiters with whitespace, the iostream delimiter of choice
	for (int i = 0; i < strstrip.size(); i++)
	{
		if (strstrip[i] == delim)
			strstrip[i] = ' ';
	}
	
	// read the array one element at a time
	std::stringstream s(strstrip);
	for (int i = 0; i < arraysize && s; i++)
	{
		s >> array[i];
	}
}

int getCrc32(const std::string & file);
void getCRC32(const char byte, int &dwCRC32);

std::string implode(const std::vector <std::string> & toImplode, const std::string & sep);
std::vector <std::string> explode(const std::string & toExplode, const std::string & sep);

void systemToFileDirSep(std::string & path);
std::string makeRelativePath(const std::string & reference, const std::string & path);
std::string makeAbsolutePath(const std::string & reference, const std::string & path);
std::string stripPath(const std::string & filepath);

bool hasExtension(const std::string & path, const std::string & extension);

#ifdef _GTKMM_H
template <typename T>
T * newWidget()
{
	T * ptr = new T();
	Gtk::manage(ptr);
	return ptr;
}
#endif

#endif
